package test.designmode.callback;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 回调的是一个队列, 触发的时候将其轮一遍
 */
public class CallbackDemo {
    public static void main(String[] args) {
        ButtonDemo button = new ButtonDemo();
        // 注册回调
        button.onClick(() -> {
            System.out.println("提示窗口");
        });
        button.onClick(() -> {
            System.out.println("报警");
        });

        // 触发
        button.click();
    }

    public interface Callback {
        void callback();
    }

    public static class ButtonDemo {
        private Queue<Callback> callbackQueue = new LinkedList<>();

        public void onClick(Callback callback) {
            callbackQueue.add(callback);
        }

        public void click() {
            System.out.println("按下了...");
            System.out.println("开始执行所有回调");
            callbackQueue.forEach(Callback::callback);
        }
    }
}
